﻿using System.ComponentModel;
using System.ComponentModel.Design;
using System.Reflection;
using System.Windows.Forms;

namespace C4F.VistaP2P.Win.Picture
{
    public class PictureDesignerActionList : DesignerActionList
    {
        public PictureDesignerActionList(IComponent component)
            : base(component)
        {

            // Automatically display smart tag panel when
            // design-time component is dropped onto the
            // Windows Forms Designer
            //this.AutoShow = true;
        }

        public override DesignerActionItemCollection GetSortedActionItems()
        {

            // Create list to store designer action items
            DesignerActionItemCollection actionItems = new DesignerActionItemCollection();


            //// Add Design category header text
            actionItems.Add(new DesignerActionHeaderItem("P2P Controls"));

            // Add Design category descriptive label
            actionItems.Add(
              new DesignerActionTextItem(
                "Properties that affect which control the Picture Control uses.",
                "Design"));

            // Dock/Undock designer action method item
            actionItems.Add(
              new DesignerActionMethodItem(
                this,
                "ToggleDockStyle",
                GetDockStyleText(),
                "Design",
                "Dock or undock this control in it's parent container.",
                true));

            actionItems.Add(
              new DesignerActionPropertyItem(
                "Visible",
                "Visible",
                GetCategory(PictureControl, "Visible"),
                GetDescription(PictureControl, "Visible")));

       
            actionItems.Add(
              new DesignerActionPropertyItem(
                "PictureBox",
                "PictureBox",
                GetCategory(this.PictureControl, "PictureBox"),
                GetDescription(this.PictureControl, "PictureBox")));

            actionItems.Add(
             new DesignerActionPropertyItem(
               "SendPictureButton",
               "Send Picture Button",
               GetCategory(this.PictureControl, "SendPictureButton"),
               GetDescription(this.PictureControl, "SendPictureButton")));

            // Return action list to design time item
            return actionItems;
        }



        public PictureBox PictureBox
        {
            get { return this.PictureControl.PictureBox; }
            set { SetProperty("PictureBox", value); }
        }

        public Button SendPictureButton
        {
            get { return this.PictureControl.SendPictureButton; }
            set { SetProperty("SendPictureButton", value); }
        }


        //// Dock/Undock designer action method implementation
        ////[CategoryAttribute("Design")]
        ////[DescriptionAttribute("Dock/Undock in parent container.")]
        ////[DisplayNameAttribute("Dock/Undock in parent container")]
        public void ToggleDockStyle()
        {

            // Toggle ClockControl's Dock property
            if (this.PictureControl.Dock != DockStyle.Fill)
            {
                SetProperty("Dock", DockStyle.Fill);
            }
            else
            {
                SetProperty("Dock", DockStyle.None);
            }
        }

        //// Helper method that returns an appropriate
        //// display name for the Dock/Undock property,
        //// based on the ClockControl's current Dock 
        //// property value
        private string GetDockStyleText()
        {
            if (this.PictureControl.Dock == DockStyle.Fill)
            {
                return "Undock in parent container";
            }
            else
            {
                return "Dock in parent container";
            }
        }

        //// Helper property to acquire a ClockControl reference
        private PictureControl PictureControl
        {
            get { return (PictureControl)this.Component; }
        }

        public bool Visible
        {
            get { return this.PictureControl.ShadowVisible; }
            set { SetProperty("ShadowVisible", value); }
        }

        // Helper method to safely set a component’s property
        private void SetProperty(string propertyName, object value)
        {
            // Get property
            PropertyDescriptor property = TypeDescriptor.GetProperties(this.PictureControl)[propertyName];
            // Set property value
            property.SetValue(this.PictureControl, value);
        }

        // Helper method to return the Category string from a
        // CategoryAttribute assigned to a property exposed by 
        //the specified object
        private static string GetCategory(object source, string propertyName)
        {
            PropertyInfo property = source.GetType().GetProperty(propertyName);
            CategoryAttribute attribute = (CategoryAttribute)property.GetCustomAttributes(typeof(CategoryAttribute), false)[0];
            if (attribute == null) return null;
            return attribute.Category;
        }

        // Helper method to return the Description string from a
        // DescriptionAttribute assigned to a property exposed by 
        //the specified object
        private static string GetDescription(object source, string propertyName)
        {
            PropertyInfo property = source.GetType().GetProperty(propertyName);
            DescriptionAttribute attribute = (DescriptionAttribute)property.GetCustomAttributes(typeof(DescriptionAttribute), false)[0];
            if (attribute == null) return null;
            return attribute.Description;
        }
    }
}
